﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsReportViolationTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// ReportforViolation Business Object
    /// </summary>

    public class ReportforViolation : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected ReportforViolation() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// ReportID.
        /// </summary>
        public int ReportID
        {
            get { return _reportID; }
            set
            {
                if (_reportID != value)
                {
                    if (OnPropertyChanging("ReportID", _reportID, value))
                    {
                        int oldValue = _reportID;
                        _reportID = value;
                        OnPropertyChanged("ReportID", oldValue, value);
                    }
                }
            }
        }  private int _reportID = -1;


        /// <summary>
        /// ObjectID.
        /// </summary>
        public string ObjectID
        {
            get { return _objectID; }
            set
            {
                if (_objectID != value)
                {
                    if (OnPropertyChanging("ObjectID", _objectID, value))
                    {
                        string oldValue = _objectID;
                        _objectID = value;
                        OnPropertyChanged("ObjectID", oldValue, value);
                    }
                }
            }
        }  private string _objectID = null;


        /// <summary>
        /// ReportedFor.
        /// </summary>
        public string ReportedFor
        {
            get { return _reportedFor; }
            set
            {
                if (_reportedFor != value)
                {
                    if (OnPropertyChanging("ReportedFor", _reportedFor, value))
                    {
                        string oldValue = _reportedFor;
                        _reportedFor = value;
                        OnPropertyChanged("ReportedFor", oldValue, value);
                    }
                }
            }
        }  private string _reportedFor = null;


        /// <summary>
        /// ReportedBy.
        /// </summary>
        public int ReportedBy
        {
            get { return _reportedBy; }
            set
            {
                if (_reportedBy != value)
                {
                    if (OnPropertyChanging("ReportedBy", _reportedBy, value))
                    {
                        int oldValue = _reportedBy;
                        _reportedBy = value;
                        OnPropertyChanged("ReportedBy", oldValue, value);
                    }
                }
            }
        }  private int _reportedBy = -1;




        /// <summary>
        /// ReportingDate.
        /// </summary>
        public DateTime? ReportingDate
        {
            get { return _reportingDate; }
            set
            {
                if (_reportingDate != value)
                {
                    if (OnPropertyChanging("ReportingDate", _reportingDate, value))
                    {
                        DateTime? oldValue = _reportingDate;
                        _reportingDate = value;
                        OnPropertyChanged("ReportingDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _reportingDate = null;



        /// <summary>
        /// Remarks.
        /// </summary>
        public string Remarks
        {
            get { return _remarks; }
            set
            {
                if (_remarks != value)
                {
                    if (OnPropertyChanging("Remarks", _remarks, value))
                    {
                        string oldValue = _remarks;
                        _remarks = value;
                        OnPropertyChanged("Remarks", oldValue, value);
                    }
                }
            }
        }  private string _remarks = null;


        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty ReportforViolation object.
        /// </summary>
        public static ReportforViolation CreateNew()
        {
            ReportforViolation obj = new ReportforViolation();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.ReportingDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static ReportforViolation CreateNew(ReportforViolationCollection collection)
        {
            ReportforViolation obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetCompleteReportforViolation

        /// <summary>
        /// Returns a collection of ReportforViolation.
        /// </summary>
        public static ReportforViolationCollection GetCompleteReportViolation()
        {
            ReportViolationTableAdapter adapter = new ReportViolationTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsReportViolation.ReportViolationDataTable tbl = adapter.GetData(null);

            ReportforViolationCollection list = new ReportforViolationCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsReportViolation.ReportViolationRow row in tbl.Rows)
                {
                    ReportforViolation reportforViolation = new ReportforViolation();
                    RelationalToObject(reportforViolation, row);
                    list.Add(reportforViolation);
                }
            }

            return (list);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified ReportforViolation.
        /// </summary>
        public static void Update(ReportforViolation obj)
        {
            bool updateReportforViolation = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateReportforViolation = (obj.ObjState != ObjectState.Unchanged);

                if (!updateReportforViolation)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsReportViolation.ReportViolationDataTable tbl = null;
            if (updateReportforViolation)
            {
                ReportViolationTableAdapter adapter = new ReportViolationTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsReportViolation.ReportViolationDataTable();
                dsReportViolation.ReportViolationRow row = tbl.NewReportViolationRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("ReportID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("ReportforViolation: ReportID={0}, ReportedBy={1}, {2}", ReportID, ReportedBy));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class ReportforViolationCollection : List<ReportforViolation>
    {
        #region Types

        public class UserComparer : IComparer<ReportforViolation>
        {
            public int Compare(ReportforViolation rv1, ReportforViolation rv2)
            {
                return (String.Compare(rv1.ReportID.ToString(), rv2.ReportID.ToString()));
            }
        }

        #endregion

        #region Public Methods

        #region FindByReportID

        public ReportforViolation FindByReportID(int reportid)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].ReportID == reportid)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion

    }
}
